﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using AMPLibrary.Controllers;

namespace AMPLibrary
{
    public class Settings
    {
        public static double Version
        {
            get;
            set;
        }

        public static double Volume
        {
            get;
            set;
        }

        public static double ApplicationWidth
        {
            get;
            set;
        }

        public static double ApplicationHeight
        {
            get;
            set;
        }

        public static double NowPlayingWidth
        {
            get;
            set;
        }

        public static int WatchFolderScanInterval
        {
            get;
            set;
        }

        public static WatchFolderScanModes WatchFolderScanMode
        {
            get;
            set;
        }

        public static bool Random
        {
            get;
            set;
        }

        public static bool Repeat
        {
            get;
            set;
        }

        public static bool Maximized
        {
            get;
            set;
        }

        public static bool FirstLoad
        {
            get;
            set;
        }

        public static bool AlwaysOnTop
        {
            get;
            set;
        }

        public static bool AssociateMP3
        {
            get;
            set;
        }

        public static bool AssociateAVI
        {
            get;
            set;
        }

        public static bool AssociateM3U
        {
            get;
            set;
        }

        public static string PlaylistFolderMusic
        {
            get;
            set;
        }

        public static string PlaylistFolderVideo
        {
            get;
            set;
        }

        public static SplittedArrayList MusicWatchFolders
        {
            get;
            set;
        }

        public static SplittedArrayList VideoWatchFolders
        {
            get;
            set;
        }

        public static SplittedArrayList MediaAssociations
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        private static void LoadDefaultSettings()
        {
            Version = 0.01;
            Random = false;
            Repeat = false;
            Volume = 5.0;
            ApplicationWidth = 650.0;
            ApplicationHeight = 450.0;
            NowPlayingWidth = 450;
            Maximized = false;
            FirstLoad = true;
            MusicWatchFolders = new SplittedArrayList();
            VideoWatchFolders = new SplittedArrayList();
            PlaylistFolderMusic = string.Empty;
            PlaylistFolderVideo = string.Empty;
            WatchFolderScanInterval = 60;
            MediaAssociations = new SplittedArrayList();
            AlwaysOnTop = false;
            WatchFolderScanMode = WatchFolderScanModes.DoNotScan;
            AssociateMP3 = false;
            AssociateAVI = false;
            AssociateM3U = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool LoadSettings()
        {
            LoadDefaultSettings();
            bool isOk = true;

            try
            {
                FileStream inputFileStream = File.Open(Paths.SettingsSaveFile, FileMode.Open);
                StreamReader streamReader = new StreamReader(inputFileStream);

                string line = streamReader.ReadLine();
                while (line != null)
                {
                    string[] keyValuePair = line.Split('=');

                    switch (keyValuePair[0])
                    {
                        case "Version":
                            Version = Convert.ToDouble(keyValuePair[1]);
                            break;

                        case "Random":
                            Random = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "Repeat":
                            Repeat = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "Volume":
                            Volume = Convert.ToDouble(keyValuePair[1]);
                            break;

                        case "ApplicationWidth":
                            ApplicationWidth = Convert.ToDouble(keyValuePair[1]);
                            break;

                        case "ApplicationHeight":
                            ApplicationHeight = Convert.ToDouble(keyValuePair[1]);
                            break;

                        case "NowPlayingWidth":
                            NowPlayingWidth = Convert.ToDouble(keyValuePair[1]);
                            break;

                        case "Maximized":
                            Maximized = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "FirstLoad":
                            FirstLoad = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "MusicWatchFolders":
                            MusicWatchFolders = new SplittedArrayList(keyValuePair[1]);
                            break;

                        case "VideoWatchFolders":
                            VideoWatchFolders = new SplittedArrayList(keyValuePair[1]);
                            break;

                        case "PlaylistFolderMusic":
                            PlaylistFolderMusic = keyValuePair[1];
                            break;

                        case "PlaylistFolderVideo":
                            PlaylistFolderVideo = keyValuePair[1];
                            break;

                        case "WatchFolderScanInterval":
                            WatchFolderScanInterval = Convert.ToInt32(keyValuePair[1]);
                            break;

                        case "MediaAssociations":
                            MediaAssociations = new SplittedArrayList(keyValuePair[1]);
                            break;

                        case "AlwaysOnTop":
                            AlwaysOnTop = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "WatchFolderScanMode":
                            WatchFolderScanMode = (WatchFolderScanModes)Convert.ToInt32(keyValuePair[1]);
                            break;

                        case "AssociateMP3":
                            AssociateMP3 = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "AssociateAVI":
                            AssociateMP3 = Convert.ToBoolean(keyValuePair[1]);
                            break;

                        case "AssociateM3U":
                            AssociateMP3 = Convert.ToBoolean(keyValuePair[1]);
                            break;
                    }

                    line = streamReader.ReadLine();
                }

                streamReader.Close();
                inputFileStream.Close();
            }
            catch
            {
                isOk = false;
            }

            return isOk;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool SaveSettings()
        {
            bool isOk = true;

            try
            {
                FileStream outputFileStream = File.Open(Paths.SettingsSaveFile, FileMode.Create);
                StreamWriter streamWriter = new StreamWriter(outputFileStream);

                streamWriter.WriteLine("Version=" + Version.ToString());                                //1
                streamWriter.WriteLine("Random=" + Random.ToString());                                  //2
                streamWriter.WriteLine("Repeat=" + Repeat.ToString());                                  //3
                streamWriter.WriteLine("Volume=" + Volume.ToString());                                  //4
                streamWriter.WriteLine("ApplicationWidth=" + ApplicationWidth.ToString());              //5
                streamWriter.WriteLine("ApplicationHeight=" + ApplicationHeight.ToString());            //6
                streamWriter.WriteLine("NowPlayingWidth=" + NowPlayingWidth.ToString());                //7
                streamWriter.WriteLine("Maximized=" + Maximized.ToString());                            //8
                streamWriter.WriteLine("FirstLoad=" + FirstLoad.ToString());                            //9
                streamWriter.WriteLine("MusicWatchFolders=" + MusicWatchFolders.ToString());            //10
                streamWriter.WriteLine("VideoWatchFolders=" + VideoWatchFolders.ToString());            //11
                streamWriter.WriteLine("PlaylistFolderMusic=" + PlaylistFolderMusic.ToString());        //12
                streamWriter.WriteLine("PlaylistFolderVideo=" + PlaylistFolderVideo.ToString());        //13
                streamWriter.WriteLine("WatchFolderScanInterval=" + WatchFolderScanInterval.ToString());//14
                streamWriter.WriteLine("MediaAssociations=" + MediaAssociations.ToString());            //15
                streamWriter.WriteLine("AlwaysOnTop=" + AlwaysOnTop.ToString());                        //16
                streamWriter.WriteLine("WatchFolderScanMode=" + ((int)WatchFolderScanMode).ToString()); //17
                streamWriter.WriteLine("AssociateMP3=" + AssociateMP3.ToString());                      //18
                streamWriter.WriteLine("AssociateAVI=" + AssociateAVI.ToString());                      //19
                streamWriter.WriteLine("AssociateM3U=" + AssociateM3U.ToString());                      //20
                
                streamWriter.Close();
                outputFileStream.Close();
            }
            catch
            {
                isOk = false;
            }

            return isOk;
        }
    }
}
